package com.yangquan.ayuan.atomic;

public class AtomicExampleProblem {
    volatile int i = 0;

//    public void incr() {
    public synchronized void incr() {
        i++;
    }
}

class App_1 {
    public static void main(String[] args) throws InterruptedException {
        AtomicExampleProblem atomicExample = new AtomicExampleProblem();
        Thread[] threads = new Thread[2];
        for (int i = 0; i < 2; i++) {
            threads[i] = new Thread(() -> {
                for (int k = 0; k < 10000; k++) {
                    atomicExample.incr();
                }
            });
            threads[i].start();
        }
        threads[0].join(); //保证线程执行结束
        threads[1].join(); //保证线程执行结束
        System.out.println(atomicExample.i);
    }
}